﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using AspDIY.Net.Common;

namespace AspDIY.Net.Web
{
    public partial class create : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string directorypath = "default";
            CreateTemplates(directorypath);
        }
        private static void CreateTemplates(string directorypath)
        {
            Hashtable ht = new Hashtable();
            ht = GetTemplates(directorypath, ht);
            foreach (string key in ht.Keys)
            {
                string filename = key.Split('.')[0];
                string[] template = ht[key].ToString().Split('\\');
                Utils.Write(filename);
                SitePageTemplate pt = new SitePageTemplate();
                pt.GetTemplate("/", template[0], filename, template.Length >= 2 ? template[template.Length - 1] : "", 1, 1);
            }
        }
        private static Hashtable GetTemplates(string directorypath,Hashtable ht)
        {
            DirectoryInfo dirinfo = new DirectoryInfo(Utils.MapPath("templates\\" + directorypath + "\\"));
            foreach (FileSystemInfo file in dirinfo.GetFileSystemInfos())
            {
                if (file.Name == "images")
                    continue;
                if (file.Attributes == FileAttributes.Directory)
                {
                    GetTemplates(directorypath + "\\" + file.Name, ht);
                }
                else
                {
                    if (file != null && (file.Extension.ToLower().Equals(".htm") || file.Extension.ToLower().Equals(".config")) && !file.Name.StartsWith("_"))
                    {
                        ht[file.Name] = directorypath;
                    }
                }
            }
            return ht;
        }
    }
}
